package com.canbot.u05.activity.guide;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zmp
 * 导览选择动作界面
 */
public class GuideActionChoiceActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private static final String TAG = "ActionChoiceActivity";

        List<ActionBean> mHandIDs = new ArrayList<>();


        private ToastTools toastTools;


        private View actionHeadBt;

        private View actionHandBt;

        private View actionLegBt;

        private ActionBean currentAction;

        private MyAdapter myAdapter;

        private String mActionID;

        private String actionName;

        {
                mHandIDs.add(new ActionBean("10101", "述说", false));
                mHandIDs.add(new ActionBean("10104", "双手上下晃动", false));
                mHandIDs.add(new ActionBean("10107", "张开双臂", false));
                mHandIDs.add(new ActionBean("10109", "请看右边", false));
                mHandIDs.add(new ActionBean("10111", "请看左边", false));
                mHandIDs.add(new ActionBean("10110", "请看右上角", false));
                mHandIDs.add(new ActionBean("10112", "请看左上角", false));
                mHandIDs.add(new ActionBean("10113", "右手曲臂", false));
                mHandIDs.add(new ActionBean("10205", "敬礼", false));
                mHandIDs.add(new ActionBean("10228", "握手", false));
                mHandIDs.add(new ActionBean("10302", "打招呼", false));
                mHandIDs.add(new ActionBean("10502", "右手数数1", false));
                mHandIDs.add(new ActionBean("10503", "右手数数2", false));
                mHandIDs.add(new ActionBean("10504", "右手数数3", false));
                mHandIDs.add(new ActionBean("10505", "右手数数4", false));
                mHandIDs.add(new ActionBean("11701", "飞吻", false));
                mHandIDs.add(new ActionBean("12101", "摇头摆手", false));
                mHandIDs.add(new ActionBean("12301", "叉腰", false));
                mHandIDs.add(new ActionBean("10507", "耍帅", false));
                mHandIDs.add(new ActionBean("13101", "石头", false));
                mHandIDs.add(new ActionBean("13102", "剪刀", false));
                mHandIDs.add(new ActionBean("13103", "布", false));
        }

        List<ActionBean> mHeadIDs = new ArrayList<>();

        {
                mHeadIDs.add(new ActionBean("10201", "看右边", false));
                mHeadIDs.add(new ActionBean("10202", "看左边", false));
                mHeadIDs.add(new ActionBean("10203", "点头", false));
                mHeadIDs.add(new ActionBean("10204", "摇头", false));
                mHeadIDs.add(new ActionBean("14001", "抬头", false));
                mHeadIDs.add(new ActionBean("14002", "低头", false));
                mHeadIDs.add(new ActionBean("11501", "看左上角", false));
                mHeadIDs.add(new ActionBean("11801", "看右上角", false));
                mHeadIDs.add(new ActionBean("14010", "仰头大笑", false));
                mHeadIDs.add(new ActionBean("14008", "左顾右盼", false));
        }

        List<ActionBean> mLegIDs = new ArrayList<>();

        {
                mLegIDs.add(new ActionBean("10206", "前进", false));
                mLegIDs.add(new ActionBean("10207", "后退", false));
                mLegIDs.add(new ActionBean("10208", "左转", false));
                mLegIDs.add(new ActionBean("10209", "右转", false));
                mLegIDs.add(new ActionBean("10210", "转过身", false));
                mLegIDs.add(new ActionBean("10211", "左转圈", false));
                mLegIDs.add(new ActionBean("10212", "右转圈", false));
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_guide_action_choice);
                toastTools = ToastTools.newInstance(this);
                Intent intent = getIntent();
                mActionID = intent.getStringExtra("actionID");
                actionName = intent.getStringExtra("actionName");
                initView();
        }

        private void initView() {
                findViewById(R.id.guide_action_set_back).setOnClickListener(this);
                findViewById(R.id.guide_action_set_save).setOnClickListener(this);
                GridView gridView = (GridView) findViewById(R.id.actionGridView);
                actionHeadBt = findViewById(R.id.actionHeadBt);
                actionHandBt = findViewById(R.id.actionHandBt);
                actionLegBt = findViewById(R.id.actionLegBt);
                View actionCustomBt = findViewById(R.id.actionCustomBt);
                actionHeadBt.setOnClickListener(this);
                actionHandBt.setOnClickListener(this);
                actionLegBt.setOnClickListener(this);
                actionCustomBt.setOnClickListener(this);
                myAdapter = new MyAdapter(this);
                gridView.setAdapter(myAdapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                currentAction = myAdapter.getItem(position);
                                mActionID = currentAction.actionID;
                                toastTools.showText(getString(R.string.action_preview));
                                MsgSendUtils.sendStringMsg(MsgType.ACTION, mActionID);
                                myAdapter.notifyDataSetChanged();
                        }
                });
                setActionAdapterByID(mActionID);
        }

        @Override
        protected void onPause() {
                super.onPause();
                MsgSendUtils.sendStringMsg(MsgType.STOP_ACTION, "stopAction");
        }

        private void setActionAdapterByID(String actionID) {
                if (TextUtils.isEmpty(actionID) || "-1".equals(actionID)) {
                        actionHeadBt.performClick();
                        return;
                }
                for (int i = 0; i < mHeadIDs.size(); i++) {
                        ActionBean actionBean = mHeadIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                currentAction = actionBean;
                                myAdapter.setList(mHeadIDs);
                                return;
                        }
                }

                for (int i = 0; i < mHandIDs.size(); i++) {
                        ActionBean actionBean = mHandIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                currentAction = actionBean;
                                myAdapter.setList(mHeadIDs);
                                return;
                        }
                }

                for (int i = 0; i < mLegIDs.size(); i++) {
                        ActionBean actionBean = mLegIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                currentAction = actionBean;
                                myAdapter.setList(mLegIDs);
                                return;
                        }
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_action_set_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.guide_action_set_save:
                                if (currentAction == null) {
                                        toastTools.showText(getString(R.string.please_select_action));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("actionID", currentAction.actionID);
                                intent.putExtra("actionName", currentAction.actionName);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;

                        case R.id.actionHeadBt:
                                if (actionHeadBt.isSelected()) {
                                        return;
                                }
                                actionHeadBt.setSelected(true);
                                actionHandBt.setSelected(false);
                                actionLegBt.setSelected(false);
                                myAdapter.setList(mHeadIDs);
                                measureBtAgain();
                                break;
                        case R.id.actionHandBt:
                                if (actionHandBt.isSelected()) {
                                        return;
                                }
                                actionHeadBt.setSelected(false);
                                actionHandBt.setSelected(true);
                                actionLegBt.setSelected(false);
                                myAdapter.setList(mHandIDs);
                                measureBtAgain();
                                break;
                        case R.id.actionLegBt:
                                if (actionLegBt.isSelected()) {
                                        return;
                                }
                                actionHeadBt.setSelected(false);
                                actionHandBt.setSelected(false);
                                actionLegBt.setSelected(true);
                                myAdapter.setList(mLegIDs);
                                measureBtAgain();
                                break;
                        case R.id.actionCustomBt:
                                toastTools.showText(getString(R.string.not_available));
                                break;
                        default:
                }
        }

        private void measureBtAgain() {
                actionHeadBt.requestLayout();
                actionHandBt.requestLayout();
                actionLegBt.requestLayout();
        }

        class ActionBean {

                private String actionID;

                private String actionName;

                private boolean isChecked;

                ActionBean(String actionID, String actionName, boolean isChecked) {
                        this.actionID = actionID;
                        this.actionName = actionName;
                        this.isChecked = isChecked;
                }
        }

        private class MyAdapter extends BaseAdapter {

                List<ActionBean> mList = Collections.synchronizedList(new ArrayList<ActionBean>());

                private final LayoutInflater layoutInflater;

                MyAdapter(Context context) {
                        layoutInflater = LayoutInflater.from(context);
                }


                @Override
                public int getCount() {
                        return mList.size();
                }

                @Override
                public ActionBean getItem(int position) {
                        return mList.get(position);
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        if (convertView == null) {
                                convertView = layoutInflater.inflate(R.layout.guide_action_item, parent, false);
                                convertView.setTag(new MyHolder(convertView));
                        }
                        ((MyHolder) convertView.getTag()).update(mList.get(position));
                        return convertView;
                }

                public void setList(List<ActionBean> mHeadIDs) {
                        mList.clear();
                        mList.addAll(mHeadIDs);
                        notifyDataSetChanged();
                }
        }


        private class MyHolder {

                private final ImageView imageView;

                private final TextView textView;

                MyHolder(View convertView) {
                        imageView = (ImageView) convertView.findViewById(R.id.checkImageView);
                        textView = ((TextView) convertView.findViewById(R.id.contentTextView));
                }

                void update(ActionBean actionBean) {
                        imageView.setSelected(actionBean.actionID.equals(mActionID));
                        textView.setText(actionBean.actionName);
                }
        }
}
